package cn.getech.data.metadata.constant;


import cn.getech.data.intelligence.common.utils.NameValue;
import lombok.Getter;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zenith
 * @Description 所有连接类型的枚举
 */
@Getter
public enum ConnectTypeEnum {

    MySQL(1, "MySQL", "com.mysql.jdbc.Driver","jdbc:mysql://%s:%s/%s"),
    Oracle(2, "Oracle", "oracle.jdbc.OracleDriver","jdbc:oracle:thin:@//%s:%s/%s"),
    SQL_Server(3 ,"SQL Server", "net.sourceforge.jtds.jdbc.Driver","jdbc:jtds:sqlserver://%s:%s;DatabaseName=%s"),
    PostgreSQL(4, "PostgreSQL", "org.postgresql.Driver","jdbc:postgresql://%s:%s/%s"),

    Hive(11,"Hive","org.apache.hive.jdbc.HiveDriver", "jdbc:hive2://%s:%s/%s"),

    // todo  没有加此类的数据源采集任务逻辑
    //SAPHANA(10, "SAP HANA", "com.sap.db.jdbc.Driver","jdbc:sap://%s:%s?reconnect=true"),
    ;
    private Integer code;
    private String name;
    private String driverClass;
    private String url;

    ConnectTypeEnum(Integer code, String name, String driverClass, String url) {
        this.code = code;
        this.name = name;
        this.driverClass = driverClass;
        this.url = url;
    }

    public static String getTypeString(Object code) {
        if (code != null && StringUtils.isNotEmpty(code.toString())) {
            for (ConnectTypeEnum value : ConnectTypeEnum.values()) {
                if (value.getCode().equals(Integer.parseInt(code.toString()))) {
                    return value.getName();
                }
            }
        }
        return null;
    }

    public static ConnectTypeEnum getDriver(Integer code) {
        if (code != null) {
            for (ConnectTypeEnum value : ConnectTypeEnum.values()) {
                if (value.getCode().equals(code)) {
                    return value;
                }
            }
        }
        return null;
    }

    public static List<NameValue<Integer>> getList() {
        List<NameValue<Integer>> list = new ArrayList<>();
        for (ConnectTypeEnum value : ConnectTypeEnum.values()) {
            NameValue<Integer> nv = new NameValue<>();
            nv.setName(value.getName());
            nv.setValue(value.getCode());
            list.add(nv);
        }
        return list;
    }


}
